package com.cooper.scraper;

import java.util.List;

import com.cooper.android.comm.Resource;

public class BrowseResourceDescCollector<T extends Resource> extends
		ResourceCollector<T> {

	private static final String BROWSE_DESC_LIST_KEY = "BROWSE_DESC_LIST_";

	public BrowseResourceDescCollector(ResourceScraper<T> scraper, int pageCount) {
		super(scraper, pageCount);
		this.pageCount = pageCount;
	}

	public BrowseResourceDescCollector(ResourceScraper<T> scraper, int pageCount, List<String> removeIds) {
		super(scraper, pageCount, removeIds);
		this.pageCount = pageCount;
	}

	@Override
	public boolean EOF() {
		return (1 < pageNumber);
	}

	@Override
	public boolean hasNextPage() {
		return (pageNumber > 1);
	}

	@Override
	public void nextPage() {
		pageNumber--;
	}

	@Override
	public void reset() {
		resetMedias();
		pageNumber = pageCount;
	}

	@Override
	public String generateKey() {
		return BROWSE_DESC_LIST_KEY + getPageNumber();
	}

	@Override
	public ResultUrl scraperListUrl() throws ScraperException,
			DownloadException {
		return scraper.createBrowseUrl(getPageNumber());
	}

	@Override
	public void scraperResult(String page, String referer) throws ScraperException,
			DownloadException {
		setMedias(scraper.extractBrowseResource(page,referer));
	}

	@Override
	public void scraperResult(String page, String referer, String redirectUrl)
			throws ScraperException, DownloadException {
		setMedias(scraper.extractBrowseResource(page, referer, redirectUrl));
	}

	@Override
	public boolean subscribed(String key) {
		return key.startsWith(BROWSE_DESC_LIST_KEY);
	}

}
